package com.whut.report;

import com.alibaba.fastjson.JSONArray;
import com.whut.common.ReportCommonFunc;
import com.whut.model.Profiles;
import com.whut.model.UrlProperties;
import com.whut.parMapper.ParProblemCollectDetailMapper;
import com.whut.parMapper.ParProblemDescriptionMapper;
import com.whut.parMapper.ParSystemMapper;
import com.whut.parMapper.utils.MapperUtils;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.utils.MDoc;
import com.whut.v_dataMap.comperhensiveInspectionDataMap.*;
import com.whut.v_vo.comperInsReportBaseVO.PartOneHeadVO;
import com.whut.v_vo.comperInsReportBaseVO.ProblemFirstListVO;
import com.whut.v_vo.comperInsReportBaseVO.ProblemFourthListVO;
import com.whut.v_vo.comperInsReportBaseVO.ProblemLastListVO;
import org.apache.commons.beanutils.BeanMap;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



public class ComperhensiveInspectionReport {
    private ParSystemMapper parSystemMapper;
    private ParProblemDescriptionMapper parProblemDescriptionMapper;
    private ParProblemCollectDetailMapper parProblemCollectDetailMapper;
    private ReportServiceImpl reportServiceImpl;
    private UrlProperties urlProperties;
    private Profiles profiles;
    private MapperUtils mapperUtils;

    private int checkId;
    private int projectId;
    private boolean isNew;
    public static final int WIDTH = 718;
    public static final int HEIGHT = 468;

    public ComperhensiveInspectionReport(ParSystemMapper parSystemMapper,
                                         ParProblemDescriptionMapper parProblemDescriptionMapper,
                                         ParProblemCollectDetailMapper parProblemCollectDetailMapper,
                                         ReportServiceImpl reportServiceImpl,
                                         MapperUtils mapperUtils,
                                         UrlProperties urlProperties,
                                         Profiles profiles){
        this.parSystemMapper = parSystemMapper;
        this.parProblemDescriptionMapper = parProblemDescriptionMapper;
        this.parProblemCollectDetailMapper = parProblemCollectDetailMapper;
        this.reportServiceImpl = reportServiceImpl;
        this.mapperUtils=mapperUtils;
        this.urlProperties=urlProperties;
        this.urlProperties=urlProperties;
        this.profiles=profiles;
    }


    public String comperhensiveInspectionReport(int checkId, int projectId) {
        ResultDataMap dataMap = new ResultDataMap("", "", "", "", new ArrayList<>(), new ArrayList<>(), new ArrayList<>());

        //第一部分 -- 表格抬头
        PartOneHeadVO partOneObj = mapperUtils.getContractTaskProjectMapper().getComInsReportPartOne(projectId, checkId);
        dataMap.setJlUnit(partOneObj.getJlUnit());
        dataMap.setProjectName(partOneObj.getProjectName());
        dataMap.setSgUnit(partOneObj.getSgUnit());
        //处理检查日期格式
        String[] checkTime = partOneObj.getCheckTime().split("-");
        dataMap.setCheckTime(checkTime[0] + "年" + checkTime[1] + "月" + checkTime[2] + "日");

        //将除表头外所需的所有数据取出
        List<ProblemFirstListVO> problemFirstList = parProblemCollectDetailMapper.getProblemFirstList(checkId, projectId);

        //文明施工list
        List<WMSGListMap> wmsgList = new ArrayList<>();
        //除文明施工外list
        List<ExceptWMSGListMap> exceptWMSGList = new ArrayList<>();
        //具体问题展示--带图片 list
        List<ProblemListMap> problemList = new ArrayList<>();

        for (ProblemFirstListVO firstObj : problemFirstList) {
            //修改pName1
            String pName1 = firstObj.getpName1();
            if (firstObj.getpName1().equals("安全检查")||firstObj.getpName1().equals("新安全检查")) {
                pName1 = "安全文明问题";
            } else if (firstObj.getpName1().equals("质量检查")||firstObj.getpName1().equals("新质量检查")) {
                pName1 = "工程质量问题";
            }

            /***
             * @description：第一部分
             */
            //除文明施工之外的检查
            ExceptWMSGListMap exceptWMSGMap = new ExceptWMSGListMap();

            //pName1
            exceptWMSGMap.setpName1(pName1);

            List<ProblemFourthListVO> fourthList = firstObj.getProblemFourthList();
            List<ProblemFourthListVO> exceptWMSG4List = fourthList.stream()
                    .filter(obj -> {
                        while (!obj.getpName4().equals("文明施工")) {
                            return true;
                        }
                        return false;
                    })
                    .collect(Collectors.toList());
            List<ExceptWMSGFourthListMap> exceptWMSGFourthList = new ArrayList<>();

            int index1_4 = 1;
            //pName4
            for (ProblemFourthListVO exceptWMSG4 : exceptWMSG4List) {
                ExceptWMSGFourthListMap exceptWMSGFourthMap = new ExceptWMSGFourthListMap();
                exceptWMSGFourthMap.setIndex(index1_4);
                exceptWMSGFourthMap.setpName4(exceptWMSG4.getpName4());
                List<ProblemLastListVO> problemLastList = exceptWMSG4.getProblemLastList();
                List<ExceptWMSGLastListMap> exceptWMSGLastList = new ArrayList<>();

                //last -- pd,checkPart
                int index = 1;
                for (ProblemLastListVO lastObj : problemLastList) {
                    ExceptWMSGLastListMap exceptWMSGLastMap = new ExceptWMSGLastListMap();
                    exceptWMSGLastMap.setIndex(index);
                    exceptWMSGLastMap.setCheckPart(lastObj.getCheckPart());
                    if (reportServiceImpl.configIsNull(lastObj.getRemark())) {
                        exceptWMSGLastMap.setRemark(lastObj.getRemark());
                    } else {
                        exceptWMSGLastMap.setRemark(lastObj.getProblemDescription());
                    }
                    exceptWMSGLastList.add(exceptWMSGLastMap);
                    ++index;
                }
                exceptWMSGFourthMap.setExceptWMSGLastList(exceptWMSGLastList);
                exceptWMSGFourthList.add(exceptWMSGFourthMap);
                index1_4++;
            }
            exceptWMSGMap.setExceptWMSGFourthList(exceptWMSGFourthList);
            exceptWMSGList.add(exceptWMSGMap);

            //将文明施工单独提出来
            List<ProblemFourthListVO> filterWMSGList = fourthList.stream()
                    .filter(obj -> obj.getpName4().equals("文明施工"))
                    .collect(Collectors.toList());
            //文明施工
            for (ProblemFourthListVO wmsg : filterWMSGList) {
                List<ProblemLastListVO> problemLastList = wmsg.getProblemLastList();
                int index = 1;
                for (ProblemLastListVO pd : problemLastList) {
                    WMSGListMap wmsgMap = new WMSGListMap();
                    wmsgMap.setIndex(index);
                    wmsgMap.setCheckPart(pd.getCheckPart());
                    if (reportServiceImpl.configIsNull(pd.getRemark())) {
                        wmsgMap.setRemark(pd.getRemark());
                    } else {
                        wmsgMap.setRemark(pd.getProblemDescription());
                    }
                    wmsgList.add(wmsgMap);
                    ++index;
                }
            }

            /***
             * @description：第二部分
             */
            ProblemListMap problemMap = new ProblemListMap();
            problemMap.setpName1(pName1);
            //pName4
            List<ProblemFourthListMap> problemFourthList = new ArrayList<>();
            int index2_4 = 1;
            for (ProblemFourthListVO problem4Obj : fourthList) {
                ProblemFourthListMap problemFourthMap = new ProblemFourthListMap();
                problemFourthMap.setIndex(index2_4);
                problemFourthMap.setpName4(problem4Obj.getpName4());
                List<ProblemLastListVO> lastList = problem4Obj.getProblemLastList();
                //pd、picName、picPath
                List<ProblemLastListMap> problemLastList = new ArrayList<>();
                int index = 1;
                for (ProblemLastListVO lastObj : lastList) {
                    ProblemLastListMap problemLastMap = new ProblemLastListMap();
                    problemLastMap.setIndex(index);
                    problemLastMap.setCheckPart(lastObj.getCheckPart());
                    if (reportServiceImpl.configIsNull(lastObj.getRemark())) {
                        problemLastMap.setRemark(lastObj.getRemark());
                    } else {
                        problemLastMap.setRemark(lastObj.getProblemDescription());
                    }
                    //处理图片
                    problemLastMap.setPicName(lastObj.getPicName());
                    problemLastMap.setPicPath(lastObj.getPicPath());
                    List<PicListMap> picList = new ArrayList<>();

                    JSONArray picNameArray = JSONArray.parseArray(problemLastMap.getPicName());
                    if (picNameArray == null || picNameArray.size() == 0) {
                        PicListMap picMap = new PicListMap();
                        //报告本地测试
//                      String s = MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png");
                        String s = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                        picMap.setProblemPic1(s);
                        picMap.setProblemPic2(s);
                        picList.add(picMap);
                    } else {
                        int j = 0;
                        while (j < picNameArray.size()) {
                            PicListMap picMap = new PicListMap();
                            //添加到最后一项
                            if (j == picNameArray.size() - 1) {
//                                String s1 = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                                String s1="";
                                if("prod".equals(profiles.getActives())){
                                     s1 = MDoc.getImageStr(problemLastMap.getPicPath() + picNameArray.getString(j));
                                }

                                picMap.setProblemPic1(s1);
                                ++j;
                            } else {
                                //报告本地测试
//                                String s1 = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
//
//                                String s2 = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                                String s1="";
                                if("prod".equals(profiles.getActives())){
                                    s1 = MDoc.getImageStr(problemLastMap.getPicPath() + picNameArray.getString(j));
                                }
                                String s2="";
                                if("prod".equals(profiles.getActives())){
                                    s2 = MDoc.getImageStr(problemLastMap.getPicPath() + picNameArray.getString(++j));
                                }
                                picMap.setProblemPic1(s1);
                                picMap.setProblemPic2(s2);
                                ++j;
                            }
                            picList.add(picMap);
                        }
                    }
                    problemLastMap.setPicList(picList);
                    problemLastList.add(problemLastMap);
                    index++;
                }
                problemFourthMap.setProblemDescriptionList(problemLastList);
                problemFourthList.add(problemFourthMap);
                index2_4++;
            }
            problemMap.setProblemFourthList(problemFourthList);
            problemList.add(problemMap);
        }
        dataMap.setExceptWMSGList(exceptWMSGList);
        dataMap.setWmsgList(wmsgList);
        dataMap.setProblemList(problemList);

        //生成报告
        DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
        String fileNameTime = dateTime.format(LocalDateTime.now());
        String reportName = dataMap.getProjectName() + "综合巡查报告" + fileNameTime;
        String ftlName = "comperhensiveInspectionReport";


        BeanMap beanMap = new BeanMap(dataMap);

        return ReportCommonFunc.generateReport(beanMap, reportName, ftlName);
    }

}
